function synchroniseViewWithModel() {
    if (model == null) {
	return;
    }

    optionRowsSelector = "#option_tbody tr[id^='option_']";
    voteBoxSelector = "#option_tbody [id^='vote::']";

    optionRowSyncer = new SortedItemSync(optionRowsSelector, addMissingRow);
    voteSyncer = new ItemSync(voteBoxSelector, addMissingVote);

    optionRowSyncer.syncElements(model.options);
    voteSyncer.syncElements(model.votes);
}


function addMissingRow(optionId, caption, sibling) {

    var rowHtml = renderModelOptionRow(optionId, caption);
    if (sibling == null) {
	$('#option_tbody').prepend(rowHtml);
    } else {
	var selector = getIdSelector(sibling);
	$(selector).after(rowHtml);
    }
    
    var option =  model.options[optionId];
    var viewerHasVoted =option.containsAnyVoteFrom(getWaveViewerId());
    if (viewerHasVoted) {
	var voteOptionSelector = "[id^='votepanel_"+optionId+"::']";
	$(voteOptionSelector).hide();
    }
    
}

function addMissingVote(voteId, vote) {
    var selector = getIdSelector('votings:' + vote.optionId + ':' + vote.answer);
    var attr = createAttribute('id', voteId);
    $(selector)
	    .append(
		    '<span '
			    + attr
			    + '>'
			    + renderViewerImage(voteId,
				    decodeURIComponent(vote.participantId))
			    + '</span>');
}


function showCaptionEditField(optionId) {
    _showCaptionEditField(optionId);
    _focusAndSelectEditFieldWithId("captionedit_" + optionId);
}

function _showCaptionEditField(optionId) {
    var form = renderCaptionEditForm(optionId);
    var containerSelector = getIdSelector("container_caption_" + optionId);
    $(containerSelector).html(form);
}

function _focusAndSelectEditFieldWithId(id) {
    var editFieldSelector = getIdSelector(id);
    $(editFieldSelector).select();
}

function hideCaptionEditField(optionId, newCaption) {
    var containerSelector = getIdSelector("container_caption_" + optionId);
    var newCaptionSpan = renderContainerCellContent(optionId, newCaption);
    $(containerSelector).html(newCaptionSpan);
}

function hideVoteOption(optionId) {
    var votePanelSelector = "[id^='votepanel_" + optionId + "::']";
    $(votePanelSelector).hide();
}

function showVoteOption(optionId) {
    var votePanelSelector = "[id^='votepanel_" + optionId + "::']";
    $(votePanelSelector).show();
}

function hideVote(voteId) {
    var voteSelector = getIdSelector(voteId);
    $(voteSelector).hide();
    
}
